<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Linkslists;
use App\Models\Linkscard;
use App\Models\Memberorders;
use App\Models\Worklink;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Cache;
use App\Services\WechatJsSdkService;
use Log,Exception;

class CardController extends Controller
{
    public function douyinCard(Request $request)
    {
        $link_type = $request->link_type;
        $id = $request->id;
        $linkCard = Linkscard::getLinksCard($id,"dy");
        if ($link_type == "wx") {
            $links = Linkslists::getLinks($id);
            $day = date("Ymd",strtotime($links->created_at));
            $linkurl = "https://s.pdb2.com/pages/".$day."/".$id.".html?from=dycard";
        } else {
            $linkurl = "https://kf.pdb2.com/kf/".$id."?from=dycard";
        }
        return view("business.card.douyin_card_iframe",["linkurl"=>$linkurl,"linkCard"=>$linkCard]);
    }

    public function douyinCardHtml(Request $request,$id)
    {
        $link_type = mb_strlen($id) >= 10 ? "wx" : "kf";
        $linkCard = Linkscard::getLinksCard($id,"dy");
        if ($link_type == "wx") {
            $links = Linkslists::getLinks($id);
            $day = date("Ymd",strtotime($links->created_at));
            $linkurl = "https://s.pdb2.com/pages/".$day."/".$id.".html?from=dycard";
        } else {
            $linkurl = "https://kf.pdb2.com/kf/".$id."?from=dycard";
        }
        return view("business.card.douyin_card_iframe",["linkurl"=>$linkurl,"linkCard"=>$linkCard]);
    }

    public function douyinCardLink(Request $request,$links_id)
    {
        if(Cache::has('link_card_'.$links_id."_dy")) {
            $linksInfo = Cache::get('link_card_'.$links_id."_dy");
        } else {
            $links = Linkslists::getLinks($links_id);
            if (empty($links)) return view('links.notice',["msg"=>"链接不存在,请核实链接是否正确！"]);
            if ($links->disable == 2) return view('links.notice',["msg"=>"链接已被删除！"]);
            $memberOrder = Memberorders::getLastLinksOrders($links->users_id);
            $product_id = empty($memberOrder) ? 1 : $memberOrder->product_id;
            $start_time = empty($memberOrder) ? time() : $memberOrder->start_time;
            $linkCard = Linkscard::getLinksCard($links_id,"dy");
            if (empty($linkCard)) return view('links.notice',["msg"=>"当前未配置抖音卡片,请配置后再访问!"]);
            $linksInfo = ["disable"=>$links->disable,"end_time"=>$links->end_time,"limit_pv"=>$links->limit_pv,"title"=>$linkCard->title,"describe"=>$linkCard->describe,"logo"=>$linkCard->logo,"start_time"=>$start_time,"product_id"=>$product_id];
            // 每次调用缓存1小时
            Cache::put('link_card_'.$links_id."_dy", $linksInfo, 1800);
        }
        if (!isset($linksInfo["end_time"])) return view('links.notice',["msg"=>"访问链接失败,请稍后尝试！"]);
        if ($linksInfo["end_time"] <= time()) {
            if ($linksInfo["limit_pv"] < 1000) {
                return view('links.notice',["msg"=>"链接体验已过期,请开通会员后使用！"]);
            } else {
                return view('links.notice',["msg"=>"链接已过期,请续费！"]);
            }
        }
        if ($linksInfo["start_time"] >= 1692892800) {
            // 在2023-08-25之前开通会员的客户可以继续使用
            if ($linksInfo["product_id"] == 2) {
                return view('links.notice',["msg"=>"基础版会员不支持卡片功能,请升级到高级版或旗舰版！"]);
            }
        }
        $status = empty($request->status) ? "jump" : $request->status;
        return view("business.card.douyin_card",["links_id"=>$links_id,"linksInfo"=>$linksInfo,"status"=>$status]);
    }

    public function cardDetial(Request $request,$links_id)
    {
        if (empty($request->type)) return view('links.notice',["msg"=>"参数错误,请获取正确的链接!"]);
        // if(Cache::has('link_card_'.$links_id."_".$request->type)) {
        //     $linksInfo = Cache::get('link_card_'.$links_id."_".$request->type);
        // } else {
            if ($request->link_type == "kf") {
                $workLink = Worklink::workLinkDetial($links_id);
                if (empty($workLink)) return view('links.notice',["msg"=>"链接不存在,请核实链接是否正确！"]);
                if ($workLink->disable == 2) return view('links.notice',["msg"=>"链接已被删除！"]);
                $users_id = $workLink->users_id;
            } else {
                $links = Linkslists::getLinks($links_id);
                if (empty($links)) return view('links.notice',["msg"=>"链接不存在,请核实链接是否正确！"]);
                if ($links->disable == 2) return view('links.notice',["msg"=>"链接已被删除！"]);
                $users_id = $links->users_id;
            }
            $memberOrder = Memberorders::getLastLinksOrders($users_id);
            $product_id = empty($memberOrder) ? 1 : $memberOrder->product_id;
            $start_time = empty($memberOrder) ? time() : $memberOrder->start_time;
            $linkCard = Linkscard::getLinksCard($links_id,$request->type);
            if (empty($linkCard)) return view('links.notice',["msg"=>"当前未配置卡片,请配置后再访问!"]);
            if ($request->link_type == "kf") {
                $linksInfo = ["disable"=>$workLink->disable,"end_time"=>$workLink->limit_time,"limit_pv"=>$workLink->limit_pv,"title"=>$linkCard->title,"describe"=>$linkCard->describe,"logo"=>$linkCard->logo,"start_time"=>$start_time,"product_id"=>$product_id];
            } else {
                $created_at =  date("Ymd",strtotime($links->created_at));
                $linksInfo = ["disable"=>$links->disable,"end_time"=>$links->end_time,"limit_pv"=>$links->limit_pv,"title"=>$linkCard->title,"describe"=>$linkCard->describe,"logo"=>$linkCard->logo,"start_time"=>$start_time,"product_id"=>$product_id,"created_at"=>$created_at];
            }
            // 每次调用缓存1小时
            Cache::put('link_card_'.$links_id."_".$request->type, $linksInfo, 1800);
        // }
        if ($linksInfo["end_time"] <= time()) {
            if ($linksInfo["limit_pv"] < 1000) {
                return view('links.notice',["msg"=>"链接体验已过期,请开通会员后使用！"]);
            } else {
                return view('links.notice',["msg"=>"链接已过期,请续费！"]);
            }
        }
        if ($linksInfo["start_time"] >= 1692892800) {
            // 在2023-08-25之前开通会员的客户可以继续使用
            if ($linksInfo["product_id"] == 2) {
                return view('links.notice',["msg"=>"基础版会员不支持卡片功能,请升级到高级版或旗舰版！"]);
            }
        }

        if ($request->type == "wx") {
            $shareParamas = WechatJsSdkService::wechatShareParamas($links_id,$request->type,$request->channel,$request->link_type);
            if ($shareParamas["status"] == "error") return view('links.notice',["msg"=>$shareParamas["msg"]]);
            if (empty($request->channel)) {
                if ($request->link_type == "kf") {
                    $shareUrl = "https://kf.pdb2.com/kf/".$links_id;
                } else {
                    $shareUrl = config("spare_urls.cardweixin")."/pages/".$linksInfo["created_at"]."/".$links_id.".html";
                }
            } else {
                if ($request->link_type == "kf") {
                    $shareUrl = "https://kf.pdb2.com/kf/".$links_id."?channel=".$request->channel;
                } else {
                    $shareUrl = config("spare_urls.cardweixin")."/pages/".$linksInfo["created_at"]."/".$links_id.".html?channel=".$request->channel;
                }
            }
            return view("business.card.weixin_card",["links_id"=>$links_id,"linksInfo"=>$linksInfo,"paramas"=>$shareParamas["paramas"],"shareUrl"=>$shareUrl]);
        } elseif ($request->type == "zh") {
            if ($request->link_type == "kf") {
                if (empty($request->channel)) {
                    $shareUrl = "https://kf.pdb2.com/kf/".$links_id;
                } else {
                    $shareUrl = "https://kf.pdb2.com/kf/".$links_id."?channel=".$request->channel;
                }
            } else {
                if (empty($request->channel)) {
                    $shareUrl = config("spare_urls.paidurl")."/pages/".$linksInfo["created_at"]."/".$links_id.".html";
                } else {
                    $shareUrl = config("spare_urls.paidurl")."/pages/".$linksInfo["created_at"]."/".$links_id.".html?channel=".$request->channel;
                }
            }
            return view("business.card.zhihu_card",["links_id"=>$links_id,"linksInfo"=>$linksInfo,"shareUrl"=>$shareUrl]);
        } else {
            return view('links.notice',["msg"=>"当前暂无本类型卡片!"]);
        }
    }
}
